﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using HMIS.MVC.CommonSupport;
using System.Web.Mvc;
using HMIS.FrameWork;
using System.ComponentModel;
using HMIS.MVC.ViewModel;
using System.Web.Security;
using HMIS.Infrastructure.Core.Authority;
using HMIS.Infrastructure.Facade.Authority;

namespace HMIS.MVC.Controllers
{
	public partial class MainController : BaseController
	{
		[ViewPage]
		public ActionResult MasterPage()
		{
			return View();
		}

		//注销登陆
		public ActionResult Logout()
		{
			FormsAuthentication.SignOut();
			Cache.RemoveSessionCache("currentuser");//清空当前用户信息缓存
			return this.JsonFormat(new ExtResult { success = true });
		}

		public class MenuTree : ExtTreeData<MenuTree>
		{
			//请求的url值
			public string url { get; set; }
			//自定义节点样式
			public string cls { get; set; }
			//是否展开
			public bool expanded { get; set; }

			public bool isExtPage { get; set; }
		}

		//返回EXT树
		[Description("获取用户菜单")]
		public ActionResult GetMenuTree()
		{
			var result = new List<MenuTree>();
			var list = new CurrentUser().MenuPermission;
			Recursion(new MenuTree { id = "0", children = new List<MenuTree>() }, result, list);
			return this.JsonFormat(result);
		}

		//递归
		private void Recursion(MenuTree parentNode, IList<MenuTree> result, IList<MenuNode> list)
		{
			string cls = "menu-node2";
			foreach (var item in from c in list where c.ParentId == parentNode.id select c)
			{
				if (item.Level == 1)
					cls = "menu-node";
				var child = new MenuTree { id = item.Id, url = item.URL, text = "[" + item.TreeCode + "]." + item.Name, expanded = true, isExtPage = item.IsExtPage, leaf = item.Leaf, iconCls = item.SmallIcon, cls = cls, children = new List<MenuTree>(), level = item.Level };
				if (item.ParentId == "0")
				{
					result.Add(child);
				}
				else
				{
					parentNode.children.Add(child);
				}
				Recursion(child, result, list);
			}
		}
	}
}
